package com.great.dao;

import com.great.domain.Cart;
import com.great.domain.Cate;
import com.great.domain.Order;
import com.great.domain.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IndexDao {

    @Select("select * from category where cate_parent_id =0")
    public List<Cate> findAllParentCate();

    @Select("select * from category where cate_parent_id !=0")
    public List<Cate> findAllChildCate();

    @Select("select * from product where product_fid=#{product_fid}")
    public List<Product>findAllfproduct(Integer product_fid);

    @Select("select * from product where product_cid=#{product_cid}")
    public List<Product> findAllcproduct(Integer product_cid);

    @Insert("insert into `cart` (user_id,product_id,cart_num) values(#{id},#{product_id},#{cart_num})")
    public void addcart(@Param("id") String id, @Param("product_id")int product_id, @Param("cart_num")int cart_num);

    @Select("select * from cart where user_id=#{user_id}")
    @Results({
            @Result(column = "user_id",property = "user_id"),
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "cart_num",property = "cart_num"),
            @Result(column = "product_id",property = "product",javaType = Product.class,many = @Many(select = "com.great.dao.ProductDao.findproductById"))
    })
    public List<Cart> findcart(String user_id);

    @Select("select count(*) from cart where user_id=#{user_id} and product_id=#{product_id}")
    public Integer checkcart(@Param("user_id") String user_id, @Param("product_id")Integer product_id);

    @Update("update cart set cart_num=#{cart_num} where user_id=#{user_id} and product_id=#{product_id}")
    public void updatecart_num(@Param("user_id") String user_id, @Param("product_id") Integer product_id, @Param("cart_num") Integer cart_num);

    @Update("update cart set cart_num=#{count} where cart_id=#{cart_id}")
    public void cart_numchange(@Param("cart_id")Integer cart_id, @Param("count")Integer count);

    @Delete("delete from cart where cart_id=#{cart_id}")
    public void cart_del(Integer cart_id);

    @Select("select * from cart where cart_id=#{cart_id}")
    @Results({
            @Result(column = "cart_id",property = "cart_id"),
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "cart_num",property = "cart_num"),
            @Result(column = "product_id",property = "product",javaType = Product.class,many = @Many(select = "com.great.dao.ProductDao.findproductById"))
    })
    public Cart searchcartrecord(Integer cart_id);

    @Insert("insert into `order` (product_id,user_id,num,price,create_time,status,address,phone,realname) Values(" +
            "#{product_id},#{user_id},#{num},#{price},#{create_time},0,#{address},#{phone},#{realname})")
    public void addorder(Order order);



    @Select("select * from `order` where user_id =#{user_id} and status !=1")
    @Results({
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "num",property = "num"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "realname",property = "realname"),
            @Result(column = "price",property = "price"),
            @Result(column = "address",property = "address"),
            @Result(column = "status",property = "status"),
            @Result(column = "create_time",property = "create_time"),
            @Result(column = "product_id",property = "product",javaType = Product.class,many = @Many(select = "com.great.dao.ProductDao.findproductById"))
    })
    public List<Order> getorder(String user_id);

    @Update("UPDATE `order` set status=1 where order_id=#{order_id}")
    public void updateorderstatus(String order_id);

    @Update("UPDATE `order` set status=2 where order_id=#{order_id}")
    public void transmit(String order_id);

    @Update("UPDATE `order` set status=3 where order_id=#{order_id}")
    public void comfim(String order_id);
}
